// componnets/buttonGroup/buttonGroup.js
import tool from '../../modules/tool'
import store from '../../modules/store'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: [] // 设置默认值为数组
    },
    title: {
      type: String,
      value: ''
    },
    type: {
      type: String,
      value: 'default'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    listDisplay: [],
    show: true,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    async refreshGroup() {
      // console.log(1, JSON.parse(JSON.stringify(this.data.listDisplay)))
      // this.setData({
      //   show: false
      // })
      // let t = this.data.listDisplay
      // this.setData({
      //   listDisplay: []
      // })
      // await tool.wait(0.01)
      // this.setData({
      //   listDisplay: t
      // })
      // this.setData({
      //   show: true
      // })
      // console.log(2, JSON.parse(JSON.stringify(this.data.listDisplay)))
    },
    async handleSwitchClick(e) {
      let value = e.detail.value
      this.handleItemClick(e, value)
    },
    async handleItemClick(e, value) {
      const handleRefreshGroup = () => {
        this.refreshGroup()
      }
      if (this.properties.list[e.currentTarget.dataset.index].disabled || (this.properties.list[e.currentTarget.dataset.index].right && this.properties.list[e.currentTarget.dataset.index].right.type == 'switch' && !e.currentTarget.dataset.isswitch)) return
      try {
        const item = this.properties.list[e.currentTarget.dataset.index];
        if (typeof item.callBack === 'function') {
          await item.callBack(handleRefreshGroup, value);
        } else {
          console.error('callBack is not a function or undefined');
        }
      } catch (error) {

      }
    },
    onChooseAvatar(e) {
      try {
        this.properties.list[e.currentTarget.dataset.index].callBack(e.detail.avatarUrl)
      } catch (error) {

      }
    }
  },

  attached() {
    tool.setPageData(this)
  }
})